# This version satisfy the feature of paper, including the execution stages, time, and buffer

from random import randint
from debug import *

resource = {}
resource["host"] = 0
resource["nfc1"] = 0
resource["nfc2"] = 0
resource["nfc3"] = 0
resource["nfc4"] = 0
# resource["nfc5"] = 0
# resource["nfc6"] = 0
# resource["nfc7"] = 0
# resource["nfc8"] = 0
# resource["nfc9"] = 0
# resource["nfc10"] = 0
# resource["nfc11"] = 0
# resource["nfc12"] = 0
# resource["nfc13"] = 0
# resource["nfc14"] = 0
# resource["nfc15"] = 0
# resource["nfc16"] = 0

# resource["matcher1"] = 0
# resource["matcher2"] = 0

resource["arm"] = 0

buffer_size = 16
write_back_time = 600
read_time = 200
matcher_time = 100

arm_time = 150

nfc_count = len(list(filter(lambda x:True if "nfc" in x else False,list(resource))))
matcher_count = len(list(filter(lambda x:True if "matcher" in x else False,list(resource))))

#每种可能操作的地址范围，因为假设任务间没有依赖所以读、文本的范围和写的范围没有重叠
read_range = (0,1000)
text_search_range = (0,1000)
write_range = (1000,2000)

read_count = 0
def read_generator():
    global read_count
    read_count += 1

    task = []
    task.append("读"+str(read_count))

    ch = randint(1,nfc_count) #选择这次读哪个channel的数据
    page = randint(read_range[0],read_range[1])
    task.append([1,"nfc"+str(ch),read_time,(0,),(ch,page)])
    task.append([2,"arm",1,(1,)])
    task.append([3,"host",20,(2,),(ch,page)])
    
    return task

write_count = 0
def write_generator():
    global write_count
    write_count += 1

    task = []
    task.append("写"+str(write_count))

    page = randint(write_range[0],write_range[1])
    ch = randint(1,nfc_count) #选择这次读哪个channel的数据
    task.append([1,"nfc"+str(ch),read_time,(0,),(ch,page)])
    task.append([2,"arm",1,(1,)])
    task.append([3,"host",20,(2,),(ch,page)]) #告诉一下写的host操作应该操作哪个buffer（这方法太挫了，罪过罪过。。。）
    
    return task

text_search_count = 0
def text_search_generator():
    global text_search_count
    text_search_count += 1

    task = []
    task.append("文本"+str(text_search_count))

    ch1 = randint(1,nfc_count)
    ch2 = randint(1,nfc_count)

    page1 = randint(text_search_range[0],text_search_range[1])
    page2 = randint(text_search_range[0],text_search_range[1])
    page3 = randint(text_search_range[0],text_search_range[1])

    task.append([1,"host",20,(0,)])  #接受参数
    task.append([2,"nfc1",read_time,(1,),(1,page1)]) #获取元数据，假设元数据都在同一个块中
    task.append([3,"arm",1,(2,)])
    task.append([4,"nfc"+str(ch1),read_time,(11,),(ch1,page2)]) #读数据块
    task.append([5,"arm",1,(4,)])
    task.append([6,"arm",arm_time,(5,)]) #进行匹配
    task.append([7,"nfc"+str(ch2),read_time,(11,),(ch2,page3)])
    task.append([8,"arm",1,(7,)])
    task.append([9,"arm",arm_time,(8,)])
    task.append([10,"host",20,(6,9)])
    task.append([11,"arm",20,(3,)])
    
    return task

generator_list = []
generator_list.append(read_generator)
generator_list.append(write_generator)
generator_list.append(text_search_generator)

task_list = []

def generate_tasks(task_count,read_r=1,write_r=1,ndp_r=1):
    #生成各自的range
    total_range = read_r+write_r+ndp_r
    read_range_list = list(range(0,read_r))
    write_range_list = list(range(read_r,read_r+write_r))
    ndp_range_list = list(range(read_r+write_r,total_range))

    task_list.clear()
    for i in range(task_count): #总共生成100个任务
        randnum = randint(0,total_range-1)
        if randnum in read_range_list:
            task_list.append(read_generator())
        elif randnum in write_range_list:
            task_list.append(write_generator())
        elif randnum in ndp_range_list:
            task_list.append(text_search_generator())

    #打印每种任务分别多少个
    print("读:",read_count)
    print("写:",write_count)
    print("文本:",text_search_count)